package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.domain.NoteComment;
import cn.wolfcode.wolf2w.business.mapper.NoteCommentMapper;
import cn.wolfcode.wolf2w.business.query.NoteCommentQuery;
import cn.wolfcode.wolf2w.business.service.INoteCommentService;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.context.SecurityContextHolder;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.member.api.RemoteUserInfoService;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 游记评论Service业务层处理
 * 
 * @author yuhao
 * @date 2025-09-16
 */
@Service
@Transactional
public class NoteCommentServiceImpl extends ServiceImpl<NoteCommentMapper,NoteComment> implements INoteCommentService {
    @Autowired
    private RemoteUserInfoService remoteUserInfoService;

    @Override
    public IPage<NoteComment> queryPage(NoteCommentQuery qo) {
        IPage<NoteComment> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
//        System.out.println( qo.getNoteId());

        // 查询
        IPage<NoteComment> page1 = lambdaQuery().eq(NoteComment::getNoteId, qo.getNoteId())
                .page(page);

        for (NoteComment item : page1.getRecords()) {
//            System.out.println( item);
            //取出NoteComment中的userId
            Long userId = item.getUserId();
            //根据userId查询UserInfo
            UserInfo data = remoteUserInfoService.getOne(userId, SecurityConstants.INNER).getData();
            //将UserInfo设置到NoteComment中
            item.setUser(data);
            //获取NoteComment中的refId
            if(item.getRefId()!=null){
                Long refId = item.getRefId();
//                System.out.println( refId);
                //获取子评论
                NoteComment refComment = baseMapper.selectById(refId);
                //在通过子评论，获取子评论的用户id，userid，
                Long userId1 = refComment.getUserId();
                //在通过用户id获取用户信息
                UserInfo data1 = remoteUserInfoService.getOne(userId1, SecurityConstants.INNER).getData();
                //在将用户id放入子评论的实体中
                refComment.setUser(data1);
                item.setRefComment(refComment);
            }

        }


        return page1;
    }

    @Override
    public void add(NoteComment noteComment) {
        Long userId = SecurityContextHolder.getUserId();
        noteComment.setUserId(userId);
        noteComment.setCreateTime(new Date());
        noteComment.setType("0");
        noteComment.setStatus("0");
        baseMapper.insert(noteComment);
    }
}
